package com.study.zzedu.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.study.zzedu.R;
import com.study.zzedu.entity.ErrorSetItem;

import org.json.JSONArray;

import java.util.List;

public class ErrorSetAdapter extends RecyclerView.Adapter<ErrorSetAdapter.ViewHolder> {
    private List<ErrorSetItem> m;


    public ErrorSetAdapter(List<ErrorSetItem> ErrorSetModel) {
        m = ErrorSetModel;
    }

    public ViewHolder onCreateViewHolder(ViewGroup parent, int Viewtype) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.error_set_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ErrorSetItem errorSetItem = m.get(position);
        if("1".equals(errorSetItem.getType())){
            holder.tvType. setText("单选");
        }else if("2".equals(errorSetItem.getType())){
            holder.tvType. setText("多选");
        }
        else if("3".equals(errorSetItem.getType())){
            holder.tvType. setText("判断");
        }
        holder.tvContent. setText(errorSetItem.getContent());
        JSONArray jArray = errorSetItem.getAnswerList();
        StringBuilder sb =  new StringBuilder();
        for (int i=0;i<jArray.length();i++){
            try {
                sb.append(jArray.get(i).toString()+",");
            }catch (Exception e){
                e.toString();
            }
        }
        holder.tvAna. setText(sb.substring(0,sb.length()-1));
    }

    public class ViewHolder extends RecyclerView.ViewHolder
    {
        private TextView tvType,tvContent,tvAna;
        public ViewHolder(View view){
            super(view);
            tvType = (TextView) view.findViewById(R.id.tv_error_type);
            tvContent = (TextView) view.findViewById(R.id.tv_error_content);
            tvAna = (TextView) view.findViewById(R.id.tv_error_ana);
        }
    }

    public int getItemCount() {
        return m.size();
    }
}